package 注解;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * @author:谢君臣
 * @Date:2021/8/9-14:50
 * @version:1.0
 * @function:
 */
public class Test注解 {
    public static void main(String[] args) throws Exception {
        //  反射创建对象
        Class<Person> Person = (Class<Person>) Class.forName("基础.注解.Person");
        Person person = Person.newInstance();
        System.out.println(person.toString());

        // 反射获取注解
        Annotation[] annotations = Person.getAnnotations();
        for (Annotation annotation : annotations) {

            System.out.println(annotation);
        }
        // 获取注解
        Data annotation = Person.getAnnotation(Data.class);
        System.out.println(annotation.value());
        // 获取所有属性
        Field[] fields = Person.getDeclaredFields();
        for (Field field : fields) {
            if (field.getAnnotation(ID.class)!=null) {
                System.out.println("主键:"+ field.getName()+";");
            }
            GenerateValue generateValue = field.getAnnotation(GenerateValue.class);
            Column column = field.getAnnotation(Column.class);
            System.out.println(field.getName()+";数据库字段名:"+column.name()+";字段类型:"+column.type()+";字段长度:"+column.length());
        }
    }
}

